package com.qiyun.dinghuohui.utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

public class JsonBuilder {
	public static final String JSON_YX = "yx";// 标识是否有效
	public static final String JSON_USERTYPE_KEY = "ut";// 用户类型
	public static final String JSON_PASSWORD_KEY = "pwd";// 密码
	public static final String JSON_ACTIVITY_KEY = "act";// 订货会ID
	public static final String JSON_OP = "op";//通讯标识
	public static final String JSON_UN = "un";//账号
	public static final String JSON_UT = "ut";// 用户类别
	public static final String JSON_DT = "dt";// 客户端时间
	public static final String JSON_CWB = "cwb";// 客户端通讯空间开始时间
	public static final String JSON_CWE = "cwe";// 客户端通讯空间结束时间
	public static final String JSON_MSG = "msg";
	public static final String JSON_Json = "json";//数据
	public static final String JSON_SYSINFO_DEVICEID = "devid";// 设备ID
	public static final String JSON_SYSINFO_BATTERY = "battery";// 电池信息

	protected JSONObject json;

	protected JSONObject getJsonRoot() {
		return json;
	}

	public JsonBuilder() {
		this.json = new JSONObject();
	}

	public JsonBuilder(JSONObject json) {
		this.json = json;
	}

	public JSONObject buildJson() {
		put(JSON_DT, DateUtil.convertDateToString(
				DateUtil.DATE_FORMAT_YYYYMMDDHHMMSS, new Date()));
		return json;
	}

	public JsonBuilder setUsername(String username) {
		put(JSON_UN, username);
		return this;
	}

	public JsonBuilder setUt(int ut) {
		put(JSON_USERTYPE_KEY, ut);
		return this;
	}

	public JsonBuilder setOp(String op) {
		put(JSON_OP, op);
		return this;
	}

	public JsonBuilder setOp(int op) {
		put(JSON_OP, op);
		return this;
	}

	public JsonBuilder setAct(int act) {
		put(JSON_ACTIVITY_KEY, act);
		return this;
	}

	public JsonBuilder setPassword(String password) {
		put(JSON_PASSWORD_KEY, password);
		return this;
	}

	public JsonBuilder setDeviceId(String devid) {
		put(JSON_SYSINFO_DEVICEID, devid);
		return this;
	}

	public JsonBuilder setBattery(String battery) {
		put(JSON_SYSINFO_BATTERY, battery);
		return this;
	}

	public JsonBuilder setPostData(JSONObject json) {
		put(JSON_Json, json);
		return this;
	}

	protected void put(String key, Object value) {
		try {
			json.put(key, value);
		} catch (JSONException e) {
			throw new RuntimeException("build post data error");
		}
	}
}
